package net.engio.mbassy.camel;

import net.engio.mbassy.MBassador;

import org.apache.camel.Consumer;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;

/**
 * Represents a Mbassador endpoint.
 */
public class MbassadorEndpoint extends DefaultEndpoint {

    /**
     * MBassador event bus object instance.
     */
    private MBassador<?> eventBus;

    /**
     * Class of events received from the Mbassador to be filtered.
     */
    private Class<?> eventClass;

    /**
     * Creates a new instance of the MbassadorEndpoint class.
     * @param uri endpoint URI
     * @param component {@link MbassadorComponent} object instance
     * @param eventBus {@link MBassador} event bus object instance
     */
    public MbassadorEndpoint(final String uri, final MbassadorComponent component, final MBassador<?> eventBus) {
        super(uri, component);
        this.eventBus = eventBus;
    }

    @Override
    public Producer createProducer() throws Exception {
        return new MbassadorProducer(this, eventBus);
    }

    @Override
    public Consumer createConsumer(final Processor processor) throws Exception {
        return new MbassadorConsumer(this, processor, eventBus, eventClass);
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    /**
     * Creates {@link Exchange} object with provided body.
     * @param event body of {@link Exchange} object
     * @return {@link Exchange} object instance
     */
    public Exchange createExchange(final Object event) {
        final Exchange exchange = createExchange();
        exchange.getIn().setBody(event);
        return exchange;
    }

    /**
     * Returns {@link MBassador} event bus object.
     * @return {@link MBassador} event bus object
     */
    public MBassador<?> getEventBus() {
        return eventBus;
    }

    /**
     * Sets {@link MBassador} event bus object.
     * @param eventBus the {@link MBassador} event bus object to set
     */
    public void setEventBus(final MBassador<?> eventBus) {
        this.eventBus = eventBus;
    }

    /**
     * Returns class of events received from the Mbassador to be filtered.
     * @return class of events
     */
    public Class<?> getEventClass() {
        return eventClass;
    }

    /**
     * Sets class of events received from the Mbassador to be filtered.
     * @param eventClass the {@link Class} of events to set
     */
    public void setEventClass(final Class<?> eventClass) {
        this.eventClass = eventClass;
    }
}
